home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 285_02 / derives.c < prev    next >
Text File  |  1990-07-08  |  3KB  |  114 lines

  1. /* Match rules with nonterminals for bison,
  2.    Copyright (C) 1984 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /* set_derives finds, for each variable (nonterminal), which rules can derive it.
  22.    It sets up the value of derives so that
  23.    derives[i - ntokens] points to a vector of rule numbers, terminated with a zero.  */
  24.  
  25. #include <stdio.h>
  26.  
  27. #include "bison.h"
  28. #include "new.h"
  29. #include "types.h"
  30. #include "gram.h"
  31. #include "derives.h"
  32.  
  33. short **derives;
  34.  
  35. void set_derives()
  36. {
  37.   register int i;
  38.   register int lhs;
  39.   register shorts *p;
  40.   register short *q;
  41.   register shorts **dset;
  42.   register shorts *delts;
  43.  
  44.   dset = NEW2(nvars, shorts *) - ntokens;
  45.   delts = NEW2(nrules + 1, shorts);
  46.  
  47.   p = delts;
  48.   for (i = nrules; i > 0; i--)
  49.     {
  50.       lhs = rlhs[i];
  51.       p->next = dset[lhs];
  52.       p->value = i;
  53.       dset[lhs] = p;
  54.       p++;
  55.     }
  56.  
  57.   derives = NEW2(nvars, short *) - ntokens;
  58.   q = NEW2(nvars + nrules, short);
  59.  
  60.   for (i = ntokens; i < nsyms; i++)
  61.     {
  62.       derives[i] = q;
  63.       p = dset[i];
  64.       while (p)
  65.     {
  66.       *q++ = p->value;
  67.       p = p->next;
  68.     }
  69.       *q++ = -1;
  70.     }
  71.  
  72. #ifdef  DEBUG
  73.   print_derives();
  74. #endif
  75.  
  76.   FREE(dset + ntokens);
  77.   FREE(delts);
  78. }
  79.  
  80. void free_derives()
  81. {
  82.   FREE(derives[ntokens]);
  83.   FREE(derives + ntokens);
  84. }
  85.  
  86.  
  87.  
  88. #ifdef  DEBUG
  89.  
  90. void print_derives()
  91. {
  92.   register int i;
  93.   register short *sp;
  94.  
  95.   extern char **tags;
  96.  
  97.   printf("\n\n\nDERIVES\n\n");
  98.  
  99.   for (i = ntokens; i < nsyms; i++)
  100.     {
  101.       printf("%s derives", tags[i]);
  102.       for (sp = derives[i]; *sp > 0; sp++)
  103.     {
  104.       printf("  %d", *sp);
  105.     }
  106.       putchar('\n');
  107.     }
  108.  
  109.   putchar('\n');
  110. }
  111.  
  112. #endif
  113.  
  114.